package com.vietnam.power.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.vietnam.power.constant.AppConfigurationConstant;
import com.vietnam.power.domain.Article;
import com.vietnam.power.domain.ArticleSearchCriteria;
import com.vietnam.power.service.ArticleService;

@Controller
public class NewsController {
	private static Logger logger = Logger.getLogger(NewsController.class);
	@Autowired
	private MessageSource messageSource;
	
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping(value = { "/searchNews" }, method = RequestMethod.GET)
	public ModelAndView searchNews(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false, value = "page") String page) {
		ModelAndView mav = new ModelAndView("client/news");

		int currentPage = 1;
		if (StringUtils.isNotBlank(page)) {
			currentPage = Integer.parseInt(page);
		}
		logger.debug(currentPage);
		ArticleSearchCriteria asc = new ArticleSearchCriteria();
		asc.setArticleGroupName(AppConfigurationConstant.ARTICLE_GROUP.NEWS);
		List<Article> artList = articleService.getArticleListByGroupName(asc, currentPage);

		mav.addObject("newsList", artList);
		mav.addObject("pageTitle", messageSource.getMessage("top.header.news", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchNewsByKeyword" }, method = RequestMethod.POST)
	public ModelAndView searchNewsByKeyword(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("client/searchNews");

		mav.addObject("pageTitle", "Trang chu");
		return mav;
	}

	@RequestMapping(value = { "/viewNews" }, method = RequestMethod.GET)
	public ModelAndView viewNews(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false, value = "id") int articleId,
			@RequestParam(required = false, value = "page") String page) {
		ModelAndView mav = new ModelAndView("client/viewNews");

		logger.debug(articleId);
		// get current article
		Article article = articleService.getArticleById(articleId);
		int currentPage = 1;
		if (StringUtils.isNotBlank(page)) {
			currentPage = Integer.parseInt(page);
		}
		// get related article
		ArticleSearchCriteria asc = new ArticleSearchCriteria();
		asc.setArticleGroupName(AppConfigurationConstant.ARTICLE_GROUP.NEWS);
		asc.setCurrentArticleId(articleId);
		List<Article> artList = articleService.getArticleListByGroupName(asc, currentPage);
		
		mav.addObject("currentArticle", article);
		mav.addObject("newsList", artList);
		mav.addObject("pageTitle", messageSource.getMessage("top.header.news", null, LocaleContextHolder.getLocale()));
		return mav;
	}

}
